/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.thuoc;

import control.thuoc.NhaCungCap_ctrl;
import control.thuoc.Thuoc_ctrl;
import gui.event.thuoc.ThuocTimKiemDlg_evt;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import model.dao.thuoc.ThuocDAO;
import model.entity.NhaCungCap;
import model.entity.Thuoc;

/**
 *
 * @author nv_ca
 */
public class ThuocTimKiem_Dialog extends javax.swing.JDialog {

    /**
     * Creates new form KHTimKiem
     *
     * @param parent
     * @param modal
     */
    public ThuocTimKiem_Dialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(parent);
        NhaCungCap_ctrl.loadAllNCC(cboxNcc);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToggleButton1 = new javax.swing.JToggleButton();
        buttonGroup1 = new javax.swing.ButtonGroup();
        pnlDieuKiemTimKiem = new javax.swing.JPanel();
        txtTimTheoTenThuoc = new javax.swing.JTextField();
        txtTimTheoMaThuoc = new javax.swing.JTextField();
        btnTimKiem = new javax.swing.JButton();
        cboxNcc = new javax.swing.JComboBox();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblThuoc = new javax.swing.JTable();
        btnIn = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();
        btnSua = new javax.swing.JButton();
        btnChiTiet = new javax.swing.JButton();
        lblStatus = new javax.swing.JLabel();

        jToggleButton1.setText("jToggleButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tìm Kiếm Thuốc");

        pnlDieuKiemTimKiem.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Điều kiện tìm kiếm", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.blue));

        txtTimTheoMaThuoc.setAutoscrolls(false);
        txtTimTheoMaThuoc.setEnabled(false);

        btnTimKiem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/images/ultility/_Search-Find-16.png"))); // NOI18N
        btnTimKiem.setText("Tìm Kiếm");
        btnTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimKiemActionPerformed(evt);
            }
        });

        cboxNcc.setEnabled(false);

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setSelected(true);
        jRadioButton1.setText("Tìm theo tên Thuốc");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("Tim theo mã thuốc");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton3);
        jRadioButton3.setText("Tìm theo nhà cung cấp");
        jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlDieuKiemTimKiemLayout = new javax.swing.GroupLayout(pnlDieuKiemTimKiem);
        pnlDieuKiemTimKiem.setLayout(pnlDieuKiemTimKiemLayout);
        pnlDieuKiemTimKiemLayout.setHorizontalGroup(
            pnlDieuKiemTimKiemLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDieuKiemTimKiemLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDieuKiemTimKiemLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButton1)
                    .addComponent(jRadioButton2)
                    .addComponent(jRadioButton3))
                .addGap(22, 22, 22)
                .addGroup(pnlDieuKiemTimKiemLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlDieuKiemTimKiemLayout.createSequentialGroup()
                        .addComponent(txtTimTheoTenThuoc, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(127, Short.MAX_VALUE))
                    .addGroup(pnlDieuKiemTimKiemLayout.createSequentialGroup()
                        .addGroup(pnlDieuKiemTimKiemLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(cboxNcc, javax.swing.GroupLayout.Alignment.LEADING, 0, 128, Short.MAX_VALUE)
                            .addComponent(txtTimTheoMaThuoc, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnTimKiem))))
        );
        pnlDieuKiemTimKiemLayout.setVerticalGroup(
            pnlDieuKiemTimKiemLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDieuKiemTimKiemLayout.createSequentialGroup()
                .addGroup(pnlDieuKiemTimKiemLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTimTheoTenThuoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRadioButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlDieuKiemTimKiemLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTimTheoMaThuoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRadioButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlDieuKiemTimKiemLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboxNcc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnTimKiem)
                    .addComponent(jRadioButton3)))
        );

        tblThuoc.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
            },
            new String [] {
                "Mã thuốc", "Tên thuốc", "Nhà cung cấp"
            })
            {public boolean isCellEditable(int row, int column){return false;}}
        );
        tblThuoc.setColumnSelectionAllowed(true);
        jScrollPane1.setViewportView(tblThuoc);
        tblThuoc.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        ListSelectionModel listSelectionModel = tblThuoc.getSelectionModel();
        listSelectionModel.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                ListSelectionModel lsm = (ListSelectionModel)e.getSource();
                btnChiTiet.setEnabled(!lsm.isSelectionEmpty());
                btnSua.setEnabled(!lsm.isSelectionEmpty());
            }});

            btnIn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/images/ultility/Printer-16.png"))); // NOI18N
            btnIn.setText("In...");
            btnIn.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnInActionPerformed(evt);
                }
            });

            btnClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/images/ultility/Close-16.png"))); // NOI18N
            btnClose.setText("Đóng");
            btnClose.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnCloseActionPerformed(evt);
                }
            });

            btnSua.setText("Sửa...");
            btnSua.setEnabled(false);
            btnSua.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnSuaActionPerformed(evt);
                }
            });

            btnChiTiet.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/images/ultility/View-Details-16.png"))); // NOI18N
            btnChiTiet.setText("Chi tiết...");
            btnChiTiet.setEnabled(false);
            btnChiTiet.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnChiTietActionPerformed(evt);
                }
            });

            lblStatus.setText("Tìm thấy: 0 kết quả");

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(lblStatus, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(btnSua, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnChiTiet)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnIn, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnClose, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(pnlDieuKiemTimKiem, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE))
                    .addContainerGap())
            );

            layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnClose, btnIn, btnSua});

            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(pnlDieuKiemTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnIn)
                        .addComponent(btnClose)
                        .addComponent(btnSua)
                        .addComponent(btnChiTiet))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(lblStatus)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );

            pack();
        }// </editor-fold>//GEN-END:initComponents

    private void btnTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTimKiemActionPerformed
        List<Thuoc> list = null;
        try {
            if (jRadioButton1.isSelected()) {
                String value = txtTimTheoTenThuoc.getText();
                list = ThuocDAO.loadByThuocName(value);
            }
            if (jRadioButton2.isSelected()) {
                int value = Integer.parseInt(txtTimTheoMaThuoc.getText());
                list = ThuocDAO.loadByThuocID(value);
            }
            if (jRadioButton3.isSelected()) {
                int value = ((NhaCungCap) cboxNcc.getSelectedItem()).getNccMa();
                list = ThuocDAO.loadByNccID(value);
            }
            Thuoc_ctrl.timKiemTableFiller(tblThuoc, list);
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Ô điều kiện phải là số nguyên.");
        }
    }//GEN-LAST:event_btnTimKiemActionPerformed

    private void btnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuaActionPerformed
        ThuocTimKiemDlg_evt.btnSuaActionPerformed(tblThuoc);
    }//GEN-LAST:event_btnSuaActionPerformed

    private void btnChiTietActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChiTietActionPerformed
        ThuocTimKiemDlg_evt.btnChiTietActionPerformed(tblThuoc);
    }//GEN-LAST:event_btnChiTietActionPerformed

    private void btnInActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInActionPerformed
        ThuocTimKiemDlg_evt.btnInActionPerformed(tblThuoc);
    }//GEN-LAST:event_btnInActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        ThuocTimKiemDlg_evt.btnCloseActionPerformed(this);
    }//GEN-LAST:event_btnCloseActionPerformed

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        txtTimTheoTenThuoc.setEnabled(true);
        txtTimTheoMaThuoc.setEnabled(false);
        cboxNcc.setEnabled(false);
    }//GEN-LAST:event_jRadioButton1ActionPerformed

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        txtTimTheoTenThuoc.setEnabled(false);
        txtTimTheoMaThuoc.setEnabled(true);
        cboxNcc.setEnabled(false);
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton3ActionPerformed
        txtTimTheoTenThuoc.setEnabled(false);
        txtTimTheoMaThuoc.setEnabled(false);
        cboxNcc.setEnabled(true);
    }//GEN-LAST:event_jRadioButton3ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ThuocTimKiem_Dialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ThuocTimKiem_Dialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ThuocTimKiem_Dialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ThuocTimKiem_Dialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                ThuocTimKiem_Dialog dialog = new ThuocTimKiem_Dialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChiTiet;
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnIn;
    private javax.swing.JButton btnSua;
    private javax.swing.JButton btnTimKiem;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cboxNcc;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JPanel pnlDieuKiemTimKiem;
    private javax.swing.JTable tblThuoc;
    private javax.swing.JTextField txtTimTheoMaThuoc;
    private javax.swing.JTextField txtTimTheoTenThuoc;
    // End of variables declaration//GEN-END:variables
}
